<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo">
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <!-- 右侧 -->
      <el-dropdown class="right-menu">
        <span class="el-dropdown-link">
          {{ userInfo.username }} 你好！<i
            class="el-icon-arrow-down el-icon--right"
          ></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>全屛</el-dropdown-item>
          <el-dropdown-item @click.native="quit">退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px' : '230px'">
        <div class="toggle">
          <i
            class="el-icon-s-unfold"
            v-if="iscollapse"
            @click="iscollapse = !iscollapse"
          ></i>
          <i
            class=" el-icon-s-fold
"
            @click="iscollapse = !iscollapse"
            v-else
          ></i>
        </div>
        <!-- 导航 -->
        <!-- 开启router会在激活导航时以 index 作为 path 进行路由跳转 -->
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo "
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          router
          :collapse="iscollapse"
          :collapse-transition="iscollapse"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + val.path"
              v-for="val in item.children"
              :key="val.id"
              >{{ val.authName }}</el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体结构 -->
      <el-main>
        <!-- 二级路由出口 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      userInfo: JSON.parse(sessionStorage.getItem("shop_userinfo")),
      menuList: [],
      iconsObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      iscollapse: false
    };
  },
  created() {
    this.getMenuList();
  },
  methods: {
    // 渲染菜单栏
    async getMenuList() {
      let { data: res } = await this.$axios.get("menus");
      // console.log(rses);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    //   退出
    quit() {
      sessionStorage.removeItem("shop_userinfo");
      this.$router.push("/login");
    }
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100vh;
}
.el-header {
  // background-color: #373d41;
  background-color: orange;
  display: flex;
  justify-content: space-between;
  .logo {
    display: flex;
    align-items: center;
    img {
      height: 50px;
      margin-right: 10px;
    }
    span {
      color: #fff;
      font-size: 20px;
    }
  }
  .right-menu {
    .el-dropdown-link {
      color: #fff;
      line-height: 60px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .toggle {
    text-align: center;
    line-height: 30px;
    background-color: #383a3d;
    i {
      margin-right: 30px;
      color: #fff;
      cursor: pointer;
    }
  }
  .el-menu {
    border-right: none;
  }
  .iconfont {
    color: orange;
    margin-right: 5px;
  }
}

.el-main {
  background-color: #eaedf1;
  height: calc(100vh - 60px);
}
</style>